﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using CarolLib;
using CarolLib.Control;
using MiniCMS.Configuration;
using MiniCMS.Business;
using MiniCMS.Model;

namespace MiniCMS.UserControls
{
    [ModuleContract("516AD2BC-433A-4A16-B778-8F62F3564B87", "编辑(&E)", "编辑", (int)PowerFlagEnmus.客户信息)]
    public partial class UCCustomerDetailInfo : MdiUserControl
    {
        #region Ctor.

        public UCCustomerDetailInfo()
            : base()
        {
            InitializeComponent();
            CheckPower(AuthUserContext.Instance.User.PowerFlag);
        }

        public UCCustomerDetailInfo(XmlNode node)
            : this()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods.

        public void ShowCustomerInfo(double Id)
        {
            var tb = OleDbDataProvider.QueryCustomerInfoTable("where 客户编号=" + Id.ToString());
            if (tb.Rows.Count == 0)
                return;

            this.tbCustomerNo.Text = tb.Rows[0]["客户编号"].ToString();
            this.tbName.Text = tb.Rows[0]["客户姓名"].ToString();
            this.tbContactNum.Text = tb.Rows[0]["联系电话"].ToString();
            this.tbPhoneNum.Text = tb.Rows[0]["手机号码"].ToString();
            this.tbConAddress.Text = tb.Rows[0]["联系地址"].ToString();
            this.tbVocation.Text = tb.Rows[0]["职业"].ToString();
            this.tbMSN.Text = tb.Rows[0]["MSN"].ToString();
            this.tbQQ.Text = tb.Rows[0]["QQ"].ToString();
            this.tbEmail.Text = tb.Rows[0]["Email"].ToString();
            this.tbPostitions.Text = tb.Rows[0]["持仓情况"].ToString();
            this.tbTradeStyles.Text = tb.Rows[0]["交易风格"].ToString();
            this.tbTradeTypes.Text = tb.Rows[0]["交易类型"].ToString();
            this.tbInvestments.Text = tb.Rows[0]["投资品种"].ToString();
            this.tbFamAddress.Text = tb.Rows[0]["家庭实际地址"].ToString();
            this.tbJointAccount.Text = tb.Rows[0]["连带账户"].ToString();
            this.tbFamProperty.Text = tb.Rows[0]["家庭财产情况"].ToString();
            this.tbFamNumbers.Text = tb.Rows[0]["家庭成员"].ToString();
            this.tbServiceChannels.Text = tb.Rows[0]["服务渠道"].ToString();
            this.tbBroker.Text = tb.Rows[0]["经纪人"].ToString();
            this.tbServices.Text = tb.Rows[0]["已经享受的服务"].ToString();
            this.tbPracticer.Text = tb.Rows[0]["实际操作人"].ToString();
            this.tbSocRelations.Text = tb.Rows[0]["社会关系"].ToString();
            this.tbFiscalAwareness.Text = tb.Rows[0]["理财意识、风险偏好"].ToString();
            this.tbCRequirements.Text = tb.Rows[0]["客户需求"].ToString();
            this.tbHobby.Text = tb.Rows[0]["爱好"].ToString();
            this.tbCharacters.Text = tb.Rows[0]["性格特征"].ToString();
            this.tbComment.Text = tb.Rows[0]["备注"].ToString();

            this.tbBornDate.Text = tb.Rows[0]["出生日期"].ToString();
            this.tbOpenDate.Text = tb.Rows[0]["开户日期"].ToString();
            this.tbCloseDate.Text = tb.Rows[0]["注销日期"].ToString();

            this.cbGender.Text = tb.Rows[0]["性别"].ToString();
            this.cbEduBackground.Text = tb.Rows[0]["学历"].ToString();

            if (tb.Rows[0]["复核状态"].ToString() == "已复核")
            {
                this.lblCheck.ForeColor = System.Drawing.Color.Blue;
                this.btnCheck.Enabled = false;
            }
            else
            {
                this.lblCheck.ForeColor = System.Drawing.Color.Red;
                this.btnCheck.Enabled = true;
            }
            this.lblCheck.Text = tb.Rows[0]["复核状态"].ToString();
        }

        private void Clear()
        {
            this.tbCustomerNo.Text = string.Empty;
            this.tbName.Text = string.Empty;
            this.tbContactNum.Text = string.Empty;
            this.tbPhoneNum.Text = string.Empty;
            this.tbConAddress.Text = string.Empty;
            this.tbVocation.Text = string.Empty;
            this.tbMSN.Text = string.Empty;
            this.tbQQ.Text = string.Empty;
            this.tbEmail.Text = string.Empty;
            this.tbPostitions.Text = string.Empty;
            this.tbTradeStyles.Text = string.Empty;
            this.tbTradeTypes.Text = string.Empty;
            this.tbInvestments.Text = string.Empty;
            this.tbFamAddress.Text = string.Empty;
            this.tbJointAccount.Text = string.Empty;
            this.tbFamProperty.Text = string.Empty;
            this.tbFamNumbers.Text = string.Empty;
            this.tbServiceChannels.Text = string.Empty;
            this.lbBroker.Text = string.Empty;
            this.tbServices.Text = string.Empty;
            this.tbPracticer.Text = string.Empty;
            this.tbSocRelations.Text = string.Empty;
            this.tbFiscalAwareness.Text = string.Empty;
            this.tbCRequirements.Text = string.Empty;
            this.tbHobby.Text = string.Empty;
            this.tbCharacters.Text = string.Empty;
            this.tbComment.Text = string.Empty;

            this.tbBornDate.Text = string.Empty;
            this.tbOpenDate.Text = string.Empty;
            this.tbCloseDate.Text = string.Empty;

            this.cbGender.Text = string.Empty;
            this.cbEduBackground.Text = string.Empty;
            this.lblCheck.Text = "未复核";
        }

        private void DisableTBs(bool enabled)
        {
            foreach (Control gp in this.Controls)
            {
                if (gp is GroupBox)
                    foreach (Control ct in (gp as GroupBox).Controls)
                    {
                        if (ct is TextBox)
                            (ct as TextBox).Enabled = enabled;
                        else if (ct is ComboBox)
                            (ct as ComboBox).Enabled = enabled;
                    }
            }
        }

        private void DisableBtns(bool enabled)
        {
            this.btnAdd.Visible = enabled;
            this.btnUpdate.Visible = enabled;
            this.btnCancel.Visible = !enabled;
            this.btnSave.Visible = !enabled;
        }

        #endregion

        #region MdiUserControl Members

        public override void Onload()
        {
            base.Onload();
            GlobalStatusContext.ShowDetail = ShowCustomerInfo;
            DisableTBs(false);
        }

        public override void Unload()
        {
            base.Unload();
            GlobalStatusContext.ShowDetail = null;
        }

        #endregion

        #region EventHandlers

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DisableTBs(true);
            DisableBtns(false);
            Clear();
            this.tbCustomerNo.Enabled = true;
            this.lblCheck.Text = "未复核";
            this.lblCheck.ForeColor = System.Drawing.Color.Red;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.tbCustomerNo.Text == string.Empty)
                return;

            DisableTBs(true);
            DisableBtns(false);
            this.tbCustomerNo.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool isAdd = this.tbCustomerNo.Enabled;

            CustomerInfo model = new CustomerInfo();

            //build model body
            model.客户编号 = double.Parse(this.tbCustomerNo.Text);
            model.客户姓名 = tbName.Text.Trim();
            model.性别 = cbGender.Text.Trim();
            model.出生日期 = tbBornDate.Text.Trim();
            model.联系电话 = tbContactNum.Text.Trim();
            model.手机号码 = tbPhoneNum.Text.Trim();
            model.联系地址 = tbConAddress.Text.Trim();
            model.学历 = cbEduBackground.Text.Trim();
            model.职业 = tbVocation.Text.Trim();
            model.开户日期 = tbOpenDate.Text.Trim();
            model.注销日期 = tbCloseDate.Text.Trim();
            model.Email = tbEmail.Text.Trim();
            model.QQ = tbQQ.Text.Trim();
            model.MSN = tbMSN.Text.Trim();
            model.投资品种 = tbInvestments.Text.Trim();
            model.交易类型 = tbTradeTypes.Text.Trim();
            model.交易风格 = tbTradeStyles.Text.Trim();
            model.持仓情况 = tbPostitions.Text.Trim();
            model.连带账户 = tbJointAccount.Text.Trim();
            model.家庭实际地址 = tbFamAddress.Text.Trim();
            model.家庭财产情况 = tbFamProperty.Text.Trim();
            model.家庭成员 = tbFamNumbers.Text.Trim();
            model.性格特征 = tbCharacters.Text.Trim();
            model.爱好 = tbHobby.Text.Trim();
            model.客户需求 = tbCRequirements.Text.Trim();
            model.理财意识风险偏好 = tbFiscalAwareness.Text.Trim();
            model.社会关系 = tbSocRelations.Text.Trim();
            model.已经享受的服务 = tbServices.Text.Trim();
            model.实际操作人 = tbPracticer.Text.Trim();
            model.经纪人 = tbBroker.Text.Trim();
            model.服务渠道 = tbServiceChannels.Text.Trim();
            model.备注 = tbComment.Text.Trim();
            model.档案修改时间 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
            model.制表人 = AuthUserContext.Instance.User.FullName;
            model.复核状态 = "未复核";
            this.lblCheck.Text = "未复核";
            this.lblCheck.ForeColor = System.Drawing.Color.Red;

            try
            {
                if (isAdd)
                {
                    if (OleDbDataProvider.InsertCustomerInfo(model) == 1)
                        MessageBox.Show("保存成功");
                    else
                        MessageBox.Show("保存失败，请检查您的输入，重复的客户编号将不能保存");
                }
                else
                    MessageBox.Show("保存成功，更新信息" + OleDbDataProvider.UpdateCustomerInfo(model) + "条");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存出错，错误信息为：" + ex.Message);
            }

            DisableTBs(false);
            DisableBtns(true);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DisableTBs(false);
            DisableBtns(true);

            if (this.tbCustomerNo.Text != "")
            {
                ShowCustomerInfo(double.Parse(this.tbCustomerNo.Text));
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (this.tbCustomerNo.Text=="")
            {
                MessageBox.Show("请选择一条记录进行复核！");
            }
            else if (lblCheck.Text == "已复核")
            {
                MessageBox.Show("该记录已复核，请选择一条未复核记录进行复核！");
            }
            else
            {
                this.lblCheck.Text = "已复核";
                this.lblCheck.ForeColor = System.Drawing.Color.Blue; CustomerInfo model = new CustomerInfo();
                model.客户编号 = double.Parse(this.tbCustomerNo.Text);
                model.复核状态 = lblCheck.Text.Trim();

                try
                {
                    MessageBox.Show("复核成功 " + OleDbDataProvider.CheckCustomerInfo(model) + "条");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("复核出错，错误信息为：" + ex.Message);
                }
            }
        }

        #endregion
    }
}
